<?php
class huifuMod extends base{
	public function __construct(){
		parent::__construct();
		//$this->auto_status();//自动审核
	}
	public function one(){
		$type=$_GET['type'];		
		$wenjian = array(
		1=>'./chuzu/*.xls',
		2=>'./chushou/*.xls'
		);
		
		$importarr = glob($wenjian[$type]);				
		$file = $importarr[0];
		$time = basename($file);
		$time = strtotime(substr($time,0, -4));
		$time = $time+3600;
		
		if(!$file){	
			if($type==1){
			echo $file;
				$this->error('出租导入完成，下面导入出售',url('huifu/one',array('type'=>2,'r'=>random())));
			}else{
				exit('wancheng');
			}
		}
		
		$excel = $this->_excelToArray($file,$type);
		if(!$excel) {echo $file;exit('shibai');}
		$db = model();
		$apartment = $db->table('apartment')->select();//户型
		$certificate = $db->table('certificate')->select();//证书
		$decoration = $db->table('decoration')->select();//装修
		$structure = $db->table('structure')->select();// 结构			
		
		
		foreach($excel as $key => $val){
			$val = $this->_htmlspecialchars($val);
			if ($type==1) {
				$insert['address']=$val[0];
				$insert['apartment']=$this->_property($val[2],$apartment,'apartment');
				$insert['shi']=$this->shi($val[3]);
				$insert['ting']=$this->ting($val[3]);
				$insert['wei']=$this->wei($val[3]);	
				$insert['chu']=$this->chu($val[3]);	
				$insert['yang']=$this->yang($val[3]);
				$insert['tu']=$this->tu($val[3]);
				$insert['floor']=$val[1];
				$insert['area']=$val[4];		
				$insert['decoration']=$this->_property($val[5],$decoration,'decoration');
				$insert['remark']=$val[6];
				$insert['rent']=$val[7];
				$insert['netid']=$val[8];
				$insert['rented']=0;
			}
			if ($type==2) {
				$insert['address']=$val[0];
				$insert['apartment']=$this->_property($val[2],$apartment,'apartment');
				$insert['shi']=$this->shi($val[3]);
				$insert['ting']=$this->ting($val[3]);
				$insert['wei']=$this->wei($val[3]);	
				$insert['chu']=$this->chu($val[3]);	
				$insert['yang']=$this->yang($val[3]);
				$insert['tu']=$this->tu($val[3]);
				$insert['floor']=$val[1];
				$insert['area']=$val[4];
				$insert['certificate']=$this->_property($val[5],$certificate,'certificate');
				$insert['structure']=$this->_property($val[6],$structure,'structure');//
				$insert['decoration']=$this->_property($val[7],$decoration,'decoration');
				$insert['remark']=$val[8];
				$insert['rent']=$val[9];
				$insert['netid']=$val[10];
				$insert['rented']=0;
			}
			$insert['uid']=$this->_G['member']['uid'];
			$insert['type']=$type;
			$insert['dateline'] = $time;
			$insert['year']=date('Y',$time);
			$insert['month']=date('n',$time);
			$insert['day']=date('j',$time);
			if (empty($insert['netid']) || empty($insert['address']) || empty($insert['apartment']) || empty($insert['rent'])) {				
				continue;					
			}
			
			$eid=$db->table('estate')->data($insert)->insert();
			
			//更新统计
			$data = array(
			'eid'=>$eid,
			'year'=>date('Y',$time),
			'month'=>date('n',$time),
			'day'=>date('j',$time),
			'username'=>$insert['netid'],
			'num'=>1
			);
			$db->table('count')->data($data)->insert();
			//更新统计end
		}
		//删除空数据
		$db->table('estate')->where("netid=''")->delete();
		$db->table('count')->where("username=''")->delete();
		unlink($file);
		
		
	
		$this->success('成功',url('huifu/one',array('type'=>$type,'r'=>random())));
			exit('chenggong');
		
			
			
	}

	public function shi($subject=''){
		if(empty($subject)) return 0;
		preg_match('/(\d+)室/i', $subject, $matches);
		return intval($matches[1]);
	}
	public function ting($subject=''){
		if(empty($subject)) return 0;
		preg_match('/(\d+)厅/i', $subject, $matches);
		return intval($matches[1]);
	}
	public function wei($subject=''){
		if(empty($subject)) return 0;
		preg_match('/(\d+)卫/i', $subject, $matches);
		return intval($matches[1]);
	}
	public function chu($subject=''){
		if(empty($subject)) return 0;
		preg_match('/(\d+)厨/i', $subject, $matches);
		return intval($matches[1]);
	}
	public function yang($subject=''){
		if(empty($subject)) return 0;
		preg_match('/(\d+)阳/i', $subject, $matches);
		return intval($matches[1]);
	}
	public function tu($subject=''){		
		if(preg_match('/凸/i', $subject)){
			return '是';
		}else{
			return '否';
		}
	}
	public function index(){
		$importarr = glob('./huifu/*.xls');
		$i=0;
		$type=$_GET['type'];
		foreach($importarr as $file){

			
			$excel = $this->_excelToArray($file,$type);
			if(!$excel) {echo $file;continue;}
			$db = model();
			$apartment = $db->table('apartment')->select();//户型
			$certificate = $db->table('certificate')->select();//证书
			$decoration = $db->table('decoration')->select();//装修
			$structure = $db->table('structure')->select();// 结构			
			$time = filemtime($file);
			foreach($excel as $key => $val){
				$val = $this->_htmlspecialchars($val);
				if ($type==1) {
					$insert['address']=$val[1];
					$insert['apartment']=$this->_property($val[2],$apartment,'apartment');
					$insert['shi']=$val[3];
					$insert['ting']=$val[4];
					$insert['wei']=$val[5];	
					$insert['chu']=$val[6];	
					$insert['yang']=$val[7];
					$insert['tu']=$val[8];
					$insert['floor']=$val[9];
					$insert['area']=$val[10];		
					$insert['decoration']=$this->_property($val[11],$decoration,'decoration');
					$insert['remark']=$val[12];
					$insert['rent']=$val[13];
					$insert['netid']=$val[14];
					$insert['rented']=0;
				}
				if ($type==2) {
					$insert['address']=$val[1];
					$insert['apartment']=$this->_property($val[2],$apartment,'apartment');
					$insert['shi']=$val[3];
					$insert['ting']=$val[4];
					$insert['wei']=$val[5];	
					$insert['chu']=$val[6];	
					$insert['yang']=$val[7];
					$insert['tu']=$val[8];
					$insert['floor']=$val[9];
					$insert['area']=$val[10];
					$insert['certificate']=$this->_property($val[11],$certificate,'certificate');
					$insert['structure']=$this->_property($val[12],$structure,'structure');//
					$insert['decoration']=$this->_property($val[13],$decoration,'decoration');
					$insert['remark']=$val[14];
					$insert['rent']=$val[15];
					$insert['netid']=$val[16];
					$insert['rented']=0;
				}
				$insert['uid']=$this->_G['member']['uid'];
				$insert['type']=$type;
				$insert['dateline'] = $time;
				$insert['year']=date('Y',$time);
				$insert['month']=date('n',$time);
				$insert['day']=date('j',$time);
				if (empty($insert['netid']) || empty($insert['address']) || empty($insert['apartment']) || empty($insert['rent'])) {				
					continue;					
				}
				$eid=$db->table('estate')->data($insert)->insert();
				
				//更新统计
				$data = array(
				'eid'=>$eid,
				'year'=>date('Y',$time),
				'month'=>date('n',$time),
				'day'=>date('j',$time),
				'username'=>$insert['netid'],
				'num'=>1
				);
				$db->table('count')->data($data)->insert();
				//更新统计end
			}
			//删除空数据
			$db->table('estate')->where("netid=''")->delete();
			$db->table('count')->where("username=''")->delete();
			unlink($file);
			echo $i++;
			//if($i==5){
			$type = $type==1 ? 2:1;
			break;
			//}
		}
		$this->success('chenggong',url('huifu/index',array('type'=>$type)));
			exit($type);
		
			
			
	}
	
	/**
	 * excel 导入
	 *
	 * @param string $file
	 * @return array
	 */
	public function import(){
		if($_FILES){
			
			$type = intval($_POST['type']);
			$file=$this->_upFile();
			$excel = $this->_excelToArray($file,$type);

			$db = model();
			$apartment = $db->table('apartment')->select();//户型
			$certificate = $db->table('certificate')->select();//证书
			$decoration = $db->table('decoration')->select();//装修
			$structure = $db->table('structure')->select();// 结构			
			$time = time();
			foreach($excel as $key => $val){
				$val = $this->_htmlspecialchars($val);
				if ($type==1) {
					$insert['address']=$val[1];
					$insert['apartment']=$this->_property($val[2],$apartment,'apartment');
					$insert['shi']=$val[3];
					$insert['ting']=$val[4];
					$insert['wei']=$val[5];	
					$insert['chu']=$val[6];	
					$insert['yang']=$val[7];
					$insert['tu']=$val[8];
					$insert['floor']=$val[9];
					$insert['area']=$val[10];		
					$insert['decoration']=$this->_property($val[11],$decoration,'decoration');
					$insert['remark']=$val[12];
					$insert['rent']=$val[13];
					$insert['netid']=$val[14];
					$insert['rented']=0;
				}
				if ($type==2) {
					$insert['address']=$val[1];
					$insert['apartment']=$this->_property($val[2],$apartment,'apartment');
					$insert['shi']=$val[3];
					$insert['ting']=$val[4];
					$insert['wei']=$val[5];	
					$insert['chu']=$val[6];	
					$insert['yang']=$val[7];
					$insert['tu']=$val[8];
					$insert['floor']=$val[9];
					$insert['area']=$val[10];
					$insert['certificate']=$this->_property($val[11],$certificate,'certificate');
					$insert['structure']=$this->_property($val[12],$structure,'structure');//
					$insert['decoration']=$this->_property($val[13],$decoration,'decoration');
					$insert['remark']=$val[14];
					$insert['rent']=$val[15];
					$insert['netid']=$val[16];
					$insert['rented']=0;
				}
				$insert['uid']=$this->_G['member']['uid'];
				$insert['type']=$type;
				$insert['dateline'] = $time;
				$insert['year']=date('Y',$time);
				$insert['month']=date('n',$time);
				$insert['day']=date('j',$time);
				if (empty($insert['netid']) || empty($insert['address']) || empty($insert['apartment']) || empty($insert['rent'])) {				
					continue;					
				}
				$eid=$db->table('estate')->data($insert)->insert();
				
				//更新统计
				$data = array(
				'eid'=>$eid,
				'year'=>date('Y',$time),
				'month'=>date('n',$time),
				'day'=>date('j',$time),
				'username'=>$insert['netid'],
				'num'=>1
				);
				$db->table('count')->data($data)->insert();
				//更新统计end
			}
			//删除空数据
			$db->table('estate')->where("netid=''")->delete();
			$db->table('count')->where("username=''")->delete();
			exit('导入成功');
			
		}

		//统计
		$time = time();
		$condition = array(
		'year'=>date('Y',$time),
		'month'=>date('n',$time),
		'day'=>date('j',$time),
		'username'=>$this->_G['member']['username'],
		);
		$db = model();

		$estate_count = $db->table('count')->where($condition)->count();
		$this->assign('today_count',$estate_count);
		$type = intval($_GET['type']) ? intval($_GET['type']) : 1;
		$all_count = $db->table('count')->where("username='{$this->_G['member']['username']}' ")->count();
		$this->assign('all_count',$all_count);
		$this->assign('type',$type);
		$this->display();
	}
	
	
	//自动添加属性
	private function _property($str,&$arr,$model){
		if (empty($str)) {
			return '';
		}
		
		//检测是否存在
		if (is_array($arr)) {
			foreach ($arr as $val){				
				if (strcasecmp($str,$val['name'])==0) {
					//var_dump($str);var_dump($val['name']);echo "<br>";
					return $val['id'];
				}
			}
		}
		//处理房屋类型,如果上面检测不存在则采用默认值
		if($model=='apartment'){
			return 1;
		}
		
		//var_dump($str);var_dump($val['name']);exit;
		//插入
		$db=model($model);
		$insert = array(
		'name'=>$str,
		'display'=>0,
		);
		$insert_id = $db->data($insert)->insert();
		//新插入数据保存到变量
		$arr[] = array(
		'id'=>$insert_id,
		'name'=>$str,
		'display'=>0,
		);
		return $insert_id;
	}
	//取得属性并用id作为key
	private function getProperty($model='apartment'){
		$db = model($model);
		$reslut = $db->order('sort desc,id asc')->select();
		if (!$reslut) return ;
		foreach ($reslut as $val){
			$return[$val['id']] = $val;
		}
		return $return;
	}
	


	
	
	
	
	
	/**
	 * excel读取返回数组
	 *
	 * @param string $file
	 * @return array
	 */
	private function _excelToArray($file,$type){
		require_once CP_PATH.'ext/PHPExcel/IOFactory.php';
		$extend = pathinfo($file);
		$extend = strtolower($extend["extension"]);
		$reader_type = array(
		'xls'=> "Excel5",
		'xlsx'=>"Excel2007",
		);
		/*require_once 'Lib/ORG/Classes/PHPExcel/IOFactory.php';
		$reader = PHPExcel_IOFactory::createReader('Excel2007'); //设置以Excel5格式(Excel97-2003工作簿)
		$PHPExcel = $reader->load("1.xlsx"); // 载入excel文件*/
		$PHPExcel = PHPExcel_IOFactory::load($file);
		$sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
		$highestRow = $sheet->getHighestRow(); // 取得总行数
		$highestColumm = $sheet->getHighestColumn(); // 取得总列数
		//$highestColumm = 'Q';
		//判断类型
//		$k2 = $sheet->getCell('O2')->getValue();
//
//		if ($type==1 && $k2!='网号') {
//			return false;
//		}
//		$i2 = $sheet->getCell('Q2')->getValue();
//
//		if ($type==2 && $i2!='网号') {			
//			return false;
//		}
		/** 循环读取每个单元格的数据 */
		for ($row = 2; $row <= $highestRow; $row++){//行数是以第2行开始
			for ($column = 'A'; $column <= $highestColumm; $column++) {//列数是以A列开始
				$dataset[$row][] = $sheet->getCell($column.$row)->getValue();
				//echo $column.$row.":".$sheet->getCell($column.$row)->getValue()."<br />";
			}
		}
		return $dataset;
	}
	
	
	private function _huxing($data){
		$str = "";
		$data['shi'] && $str .= $data['shi']."室";
		$data['ting'] && $str .= $data['ting']."厅";
		$data['wei'] && $str .= $data['wei']."卫";
		$data['chu'] && $str .= $data['chu']."厨";
		$data['yang'] && $str .= $data['yang']."阳";
		if($data['tu']=='是') $str .= "凸";
		return $str;
	}

}